package cae.logic.destination;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class CaeDestinatiosManager 
{
	private static CaeDestinatiosManager instance;
	private List<CaeDestination> destinations;
	private static final String DESTINATIONS_FILE = "destinations.xml";
	private static XStream xstream = new XStream(new DomDriver());
	private static Logger logger = Logger.getLogger(CaeDestinatiosManager.class);
	
	public static CaeDestinatiosManager getInstance()
	{
		if(instance == null)
			instance = new CaeDestinatiosManager();
		return instance;
	}
	
	private CaeDestinatiosManager()
	{
		xstream.alias("destination", CaeDestination.class);
		load();
	}

	@SuppressWarnings("unchecked")
	private void load()
	{
		try
		{
			InputStream file = new FileInputStream(DESTINATIONS_FILE);
			destinations = (List<CaeDestination>)xstream.fromXML(file);
			file.close();
		}
		catch(Exception e)
		{
			logger.fatal("Error en la carga del archivo de destinations ", e);
			throw new RuntimeException("Error en la carga del archivo de destinations ", e);
		}
	}
	
	private void save()
	{
		try
		{
			OutputStream file = new FileOutputStream(DESTINATIONS_FILE);
			xstream.toXML(destinations, file);
			file.close();
		}
		catch(Exception e)
		{
			logger.fatal("Error en la escritura del archivo de destinations ", e);
			throw new RuntimeException("Error en la escritura del archivo de destinations ", e);
		}
	}

	public List<CaeDestination> getDestinations() {
		return destinations;
	}
	
	public void addDestination(CaeDestination dest) throws CaeDestinationException
	{
		if( getDestination(dest.getMachine()[0]) != null)
			throw new CaeDestinationException("El destination ya existe");
		destinations.add(dest);
		save();
	}
	
	public CaeDestination getDestination(String machineName)
	{
		for(CaeDestination d : destinations)
		{
			String[] machineNames = d.getMachine();
			for (String destMachineName : machineNames) {
				if(destMachineName.equalsIgnoreCase(machineName))
				{
					return d;
				}
			}
		}
		return null;
	}

	public CaeDestination getDestinationByAlias(String alias) 
	{
		for(CaeDestination d : destinations)
		{
			if(d.getAlias().equalsIgnoreCase(alias))
			{
				return d;
			}
		}
		return null;
	}

	public void removeDestination(CaeDestination dest) {
		destinations.remove(dest);
		save();
	}
	
	
	
	public static void main(String[] args) throws FileNotFoundException {
		List<CaeDestination> dest = new ArrayList<CaeDestination>();
		dest.add(new CaeDestination("pepe", "pepe", 1,1,1));
		xstream.toXML(dest, new FileOutputStream("a.xml"));
	}
}
